package com.boo2.controller;

import com.boo2.entity.Student;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author wzm
 * @create 2024-02-26 16:56
 * project_name:JavaWeb2401
 * @Description
 */
@RestController
public class RequestController {

    //一、接收简单的参数

    //1.使用原生servlet方式
    @RequestMapping("/simpleParam")
    public String simple1(HttpServletRequest request) {
        String name = request.getParameter("name");
        String age = request.getParameter("age");

        System.out.println("name = " + name);
        System.out.println("age = " + age);


        return "ok";
    }

    //2.SpringBoot方式接收简单参数
    @RequestMapping("/simpleParam01")
    public String simple2(String name, Integer age) { //形参名和请求路径上参数名一致
        System.out.println("name888 = " + name);
        System.out.println("age = " + age);


        return "ok";
    }

    //请求参数name与方法形参username不一致
    // @RequestParam("name") 用注解中的这个请求参数的值赋值给对应的形参
    //@RequestParam("name") 注解中 要求参数必须传否则400 boolean required() default true;
    //可以设置为false就非必须 但是value可以省略 写多个属性必须写出来
    //@RequestParam(value = "name",required = false) String username
    @RequestMapping("/simpleParam02")
    public String simple3(@RequestParam(value = "name", required = false) String username, Integer age) {
        System.out.println("name888 = " + username);
        System.out.println("age = " + age);
        return "ok";
    }

    //二、实体参数

    //保证请求参数和方法形参名一致可以接受数据 但是如果参数非常多就会特别繁琐 此时我们可以考虑用实体来进行封装
    //1.简单实体对象  请求参数名与实体类中的属性名相同 自动封装
    @RequestMapping("/pojo")
    public String pojo(Student student) {
        System.out.println("student = " + student);
        return "ok";
    }


    //2.参数名和实体属性名不一致  修改请求参数名一致  或单独接收
    @RequestMapping("/pojo01")
    public String pojo01(Student student, String username) {
//        student.setName(username);
        System.out.println("student = " + student);
        return "ok";
    }

    //三、数组集合参数
    //1.数组参数
    @RequestMapping("/array")
    public String array(String[] hobby) {
        System.out.println(Arrays.toString(hobby));

        return "ok";
    }

    //2.集合参数 加上@RequestParam才能识别
    @RequestMapping("/list/")
    public String list(@RequestParam List<String> hobby) {
        System.out.println(hobby);

        return "ok";
    }

    //四 日期参数
    @RequestMapping("/dateParam")
    public String dateParam(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime) {
        System.out.println("updateTime = " + updateTime);

        return "ok";
    }

    //五 json参数
    //@RequestBody 将json数据映射到形参的实体类对象中 JSON数据传递key与对象属性要一致
    @RequestMapping("/json")
    public String json(@RequestBody Student student) {
        System.out.println("student = " + student);
        return "ok";
    }


    //六 路径参数
    //@PathVariable  路径变量
    //前端传递参数可能使用路径参数 我们使用{key}在访问路径上站位  用@PathVariable 注解标记参数绑定到形参
    @RequestMapping("/page/{id}/{current}/{hx}")
    public String pathParm(@PathVariable("id") Integer size,@PathVariable Integer current,@PathVariable String hx) {
        System.out.println("size = " + size);
        System.out.println("current = " + current);
        return "ok";
    }


}